# Maintainer: Manuel Naranjo <naranjo.manuel@gmail.com>

_pyname=psutil
_realname=${_pyname}
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=5.4.6
pkgrel=1
pkgdesc="A cross-platform process and system utilities module for Python (mingw-w64)"
arch=('any')
url="https://github.com/giampaolo/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::"${url}/${_realname}/archive/release-${pkgver}.tar.gz"
        '0001-MSYS-Allow-detecting-when-running-on-MSYS.patch'
        '0002-msys-fixes-for-msys2.patch')
sha256sums=('1fbe56d7937410837ff4f8a250a9d80fcb652982982a0a2a999f906bcea4bafc'
            '8087aefe39b9037a7bd8dec2c51007da6b8aa92f8e9b45c221df0dbe8454ca5c'
            'fd5c0547eac613e63aca1a1aba160f955971620c116332edac4cfc5be62acddb')

prepare() {
  cd "${srcdir}"/${_realname}-release-${pkgver}
  patch -p1 -i "${srcdir}/0001-MSYS-Allow-detecting-when-running-on-MSYS.patch"
  patch -p1 -i "${srcdir}/0002-msys-fixes-for-msys2.patch"
  cd ${srcdir}

  for builddir in python{2,3}-build; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-release-${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done
}

# Note that build() is sometimes skipped because it's done in
# the packages setup.py install for simplicity if you can do so.
package_python3-psutil() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_python2-psutil() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-psutil() {
  package_python2-psutil
}

package_mingw-w64-i686-python3-psutil() {
  package_python3-psutil
}

package_mingw-w64-x86_64-python2-psutil() {
  package_python2-psutil
}

package_mingw-w64-x86_64-python3-psutil() {
  package_python3-psutil
}
